//
//  TPUserCollectionViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/26.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPUserCollectionViewController.h"
#import "TPProductDetailViewController.h"
#import "TPHomeCell.h"
#import "TPHomeModel.h"
@interface TPUserCollectionViewController ()

@end

@implementation TPUserCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    [self setup];
}
-(void)setup{
    
    @weakify(self);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self getRequest];
    [self.tableView xw_addPullToRefreshWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page = 1;
        [self getRequest];
        
    }];
    
    [self.tableView xw_addInfinityScrollWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page ++;
        [self getRequest];
    }];
}

-(void)getRequest{
    
    [self.view tp_hideNotMsg];
    [[TPRequestClient sharedClient]getUserCollection_current:self.page size:kMaxRequestSize success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPHomeModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self refreshSccuess];
        
        if(self.dataSource.count == 0){
            [self.view tp_addNotMsg:@"还没有任何收藏数据~"];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [self refreshFail];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self refreshFail];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TPHomeCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellx"];
    if(!cell){
        cell = [[TPHomeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellx"];
    }
    cell.is_collection = YES;
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TPProductDetailViewController *detailVC = [TPProductDetailViewController new];
    detailVC.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TPHomeModel *model = self.dataSource[indexPath.row];
    [[TPRequestClient sharedClient]getCollectionCreateId:model.goods_id  isAdd:NO success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [self.dataSource removeObject:model];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [self.view xw_hideTitle:respone.message];
        
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        ;
    }];
    
}

@end
